# -*- coding: utf-8 -*-
class HouseController < ApplicationController
  before_filter :user_authorize
  before_filter :sys_before_log
  
  #每个要交费用的单元的相关Action
  #根据条件查找出相关的单元
  
  def findHouse
    
  end

  #发现欠费的用户
  def getOwedOwners


  end

  #这是Grid通过DS调用的方法，目的是得到相关的业主信息
  def getAllHouses
    
    action_type = params[:action_type]
    puts action_type
    #如果有houseId说明用户是按业主信息搜索的。
    if action_type == "house"
      houseId = params[:house_id]
      houses = House.where("id = ? AND is_locked = 0",houseId)
    elsif action_type == "owed"#欠费查询
      endYear = params[:end_year].to_i
      endMonth = params[:end_month].to_i
      endDate = endYear*100 + endMonth
      owedType = params[:owed_type]
      buildingId = params[:building_id].to_i
      if owedType =="物业"
        houses = House.where("building_id = ? AND is_locked = 0 AND (property_pay_year*100 + property_pay_month) < ?",buildingId,endDate)
      elsif owedType =="电梯"
        houses = House.where("building_id = ? AND is_locked = 0 AND (lift_pay_year*100 + lift_pay_month) < ?",buildingId,endDate)
      elsif owedType =="水费"
        houses = House.where("building_id = ? AND is_locked = 0 AND (water_pay_year*100 + water_pay_month) < ?",buildingId,endDate)
      end
      puts params
    else
      buildingId = params[:building_id]
      puts buildingId
      if buildingId.length>0
        houses = House.where("building_id = ? AND is_locked = 0",buildingId)
      else
        houses = House.where("is_locked = 0")
      end
    end
    jsonStr="{totalProperty:"+houses.length.to_s+",root:"+houses.to_json(:include=>[:building])+"}"
    puts jsonStr
    render :text=>jsonStr, :layout=>false
  end

  
  #这是Grid通过DS调用的方法，目的是得到相关的业主信息,管理介面使用，所以不需要is_locked条件
  def getAllHousesFromAdmin    
    action_type = params[:action_type]
    #如果有houseId说明用户是按业主信息搜索的。
    if action_type == "house"
      houseId = params[:house_id]
      houses = House.where("id = ?",houseId)
    else
      buildingId = params[:building_id]
      if buildingId.to_i > 0
        houses = House.where("building_id = ?",buildingId)
      else
        houses = House.all
      end
    end
    jsonStr="{totalProperty:"+houses.length.to_s+",root:"+houses.to_json(:include=>[:building])+"}"
    puts jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #这是combobox用的，通过combobox的ds调用的方法。目的是模糊查询的方式得到所有业主。
  def getAllHouseByOwnerInfo
    query = params[:query]
    
    if query.length > 0
      query = "%"+query+"%"
      houses = House.where("number like ? OR owner like ?",query,query)
    else
      houses = House.all
    end
    jsonStr="{totalProperty:"+houses.length.to_s+",root:"+houses.to_json()+"}"

    puts jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #House 加锁与解锁
  def houseLock
    house = House.find(params[:house_id])
    if house.id >1
      if house.is_locked == 1
        house.is_locked = 0
      else
        house.is_locked = 1
      end
      house.save
    end
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def createHouse    
    puts params
    building = Building.find(params[:building_id])
    house = House.new
    house.building_id = building.id
    house.number = building.number + "-" + params[:doorplate]
    house.owner = params[:owner]
    house.phone = params[:phone]
    house.move_in = params[:move_in]
    house.area = params[:area]
    house.water_last_meter =  params[:water_last_meter]
    house.balance = params[:balance]
    house.doorplate = params[:doorplate]
    house.user_id = session[:user_id]
    
    house.property_pay_year = params[:property_pay_year]
    house.property_pay_month = params[:property_pay_month]
    if params[:lift_pay_year] == nil
      house.lift_pay_year = 2004
    else
      house.lift_pay_year = params[:lift_pay_year]
    end
    
    if params[:lift_pay_month]==nil
      house.lift_pay_month = 1
    else
      house.lift_pay_month = params[:lift_pay_month]
    end

    if params[:water_pay_year] ==nil
      house.water_pay_year = 2004
    else
      house.water_pay_year = params[:water_pay_year]
    end

    if params[:water_pay_month] ==nil
      house.water_pay_month = 1
    else
      house.water_pay_month = params[:water_pay_month]
    end

    house.save
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def editHouse
    @houseId = params[:house_id]
  end

  def getHouseById
    
    house = House.find(:first,:conditions=>["id = ?",params[:house_id]])
    jsonStr = "{root:["+house.to_json(:include=>[:building])+"]}"
    puts jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def updateHouse
    
    puts params
    if params[:building_id].include? "-"
      building = nil
    else
      building = Building.find(params[:building_id])
    end
    house = House.find(params[:id])
    #如果不等于nil说明用户在编辑时，从新选择了楼座
    if building != nil
      house.building_id = building.id
      house.number = building.number + "-" + params[:doorplate]
    else
      building = Building.find(house.building_id)
      house.number = building.number + "-" + params[:doorplate]
    end
    house.owner = params[:owner]
    house.phone = params[:phone]
    house.move_in = params[:move_in]
    house.area = params[:area]
    house.water_last_meter =  params[:water_last_meter]
    house.balance = params[:balance]
    house.doorplate = params[:doorplate]
    house.user_id = session[:user_id]

    house.property_pay_year = params[:property_pay_year]
    house.property_pay_month = params[:property_pay_month]
    if params[:water_pay_year] !=nil
      house.water_pay_year = params[:water_pay_year]
    end
    if params[:water_pay_month] !=nil
      house.water_pay_month = params[:water_pay_month]
    end
    if params[:lift_pay_year] != nil
      house.lift_pay_year = params[:lift_pay_year]
    end
    if params[:lift_pay_month] !=nil
      house.lift_pay_month = params[:lift_pay_month]
    end   
    
    house.save
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

end
